package source;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class TorneoDataAccess {

	Connection connection;


	public TorneoDataAccess() {

		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost/poliholdem";
			connection = DriverManager.getConnection(url, "phm", "phm");      
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	/**
	 * 
	 * @param id_t
	 * @param luogo
	 * @param num_partecip
	 * @param data
	 * @return
	 * @throws SQLException
	 */
	public boolean updateTorneo(int id_t, String luogo, int num_partecip,
			String data) throws SQLException{


		PreparedStatement stm = connection.prepareStatement("UPDATE torneo"+
		" SET data=?, luogo=?, num_partecip=? WHERE id_torneo=?");

		stm.setString(1,data);
		stm.setString(2,luogo);
		stm.setInt(3,num_partecip);
		stm.setInt(4,id_t);

		return !stm.execute();
	}


	
	/**
	 * 
	 * @param id_t
	 * @param luogo
	 * @param num_partecip
	 * @param data
	 * @return
	 * @throws SQLException
	 */
	public boolean insertNewTorneo(int id_t, String luogo, int num_partecip,
			String data) throws SQLException{


		if (existTorneo(id_t)){
			System.out.println("ERROR! Id del nuovo torneo già esistente.");
			return false;
		}

		PreparedStatement stm = connection.prepareStatement("INSERT INTO torneo"+
		" VALUES(?, ?, ?, ?)");

		stm.setInt(1,id_t);
		stm.setString(2,data);
		stm.setString(3,luogo);
		stm.setInt(4,num_partecip);

		return !stm.execute();
	}

	
/**
 * 
 * @param id_t
 * @return true if only 1 entry has been found with id_t, false otherwise
 * @throws SQLException
 */
	public boolean existTorneo(int id_t) throws SQLException {

		ArrayList<Torneo> result = new ArrayList<Torneo>();
		PreparedStatement stm = connection.prepareStatement("SELECT * FROM torneo"+
		" WHERE id_torneo=?");
		stm.setInt(1, id_t);
		ResultSet rs = stm.executeQuery();

		while (rs.next()) {
			Torneo torneo = new Torneo(rs.getInt("id_torneo"), rs.getString("data"), 
					rs.getString("luogo"), rs.getInt("num_partecipanti"));
			result.add(torneo);
		}
		if(result.isEmpty())
			return false;
		if(result.size()>1){
			System.out.println("ERROR! More than one torneo found with id="+id_t);
			return false;
		}

		return true;
	}
	
	
	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public int getNumTorneiTotDisputati() throws SQLException {
		
		int n=0;
		PreparedStatement stm = connection.prepareStatement("SELECT count(id_torneo) FROM torneo");
		ResultSet rs = stm.executeQuery();
		
		while (rs.next()){
			n = rs.getInt("count(id_torneo)");
		}
		return n;	
		
	}


	/**
	 * 
	 * @param idT
	 * @return
	 * @throws SQLException
	 */
	public boolean deleteTorneo(int idT) throws SQLException {
		
		PreparedStatement stm = connection.prepareStatement("DELETE FROM poliholdem.torneo " +
				"WHERE id_torneo = ?");
		stm.setInt(1, idT);	

		return !stm.execute();
		
	}
	

}
